package com.huawei.zyb.taskManager.interfaces.rest;

import com.huawei.zyb.taskManager.application.developer.DeveloperApplicationService;
import com.huawei.zyb.taskManager.application.project.ProjectApplicationService;
import com.huawei.zyb.taskManager.application.requirement.RequirementApplicationService;
import com.huawei.zyb.taskManager.application.task.TaskApplicationService;
import com.huawei.zyb.taskManager.application.developer.dto.DeveloperDTO;
import com.huawei.zyb.taskManager.application.project.dto.ProjectDTO;
import com.huawei.zyb.taskManager.application.requirement.dto.RequirementDTO;
import com.huawei.zyb.taskManager.application.task.dto.TaskDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

/**
 * 调试控制器
 * 用于提供调试页面，显示后端数据以便测试和验证
 */
@Controller
public class DebugController {

    @Autowired
    private DeveloperApplicationService developerApplicationService;

    @Autowired
    private ProjectApplicationService projectApplicationService;

    @Autowired
    private RequirementApplicationService requirementApplicationService;

    @Autowired
    private TaskApplicationService taskApplicationService;

    /**
     * 显示调试页面
     * 获取所有数据并传递给模板进行渲染
     */
    @GetMapping("/debug")
    public String debug(Model model) {
        try {
            // 获取所有开发者
            List<DeveloperDTO> developers = developerApplicationService.findAll();
            model.addAttribute("developers", developers);
            model.addAttribute("totalDevelopers", developers.size());

            // 获取所有项目
            List<ProjectDTO> projects = projectApplicationService.findAll();
            model.addAttribute("projects", projects);
            model.addAttribute("totalProjects", projects.size());

            // 获取所有需求
            List<RequirementDTO> requirements = requirementApplicationService.findAll();
            model.addAttribute("requirements", requirements);
            model.addAttribute("totalRequirements", requirements.size());

            // 获取所有任务
            List<TaskDTO> tasks = taskApplicationService.findAllTasks();
            model.addAttribute("tasks", tasks);
            model.addAttribute("totalTasks", tasks.size());

        } catch (Exception e) {
            // 如果出现异常，记录错误并设置默认值
            System.err.println("调试页面数据加载出错: " + e.getMessage());
            e.printStackTrace();
            
            model.addAttribute("developers", List.of());
            model.addAttribute("projects", List.of());
            model.addAttribute("requirements", List.of());
            model.addAttribute("tasks", List.of());
            model.addAttribute("totalDevelopers", 0);
            model.addAttribute("totalProjects", 0);
            model.addAttribute("totalRequirements", 0);
            model.addAttribute("totalTasks", 0);
            model.addAttribute("error", "数据加载失败: " + e.getMessage());
        }

        return "debug";
    }
}